"""
Case Type   : 服务端工具
Case Name   : root用户 gs_check 检查视图报错查看报错文件
Create At   : 2024-10-9
Owner       : ningyali
Description :
    1.root用户进入安装目录下的script目录
    2.在非本地模式下运行openGauss的用户检查指定检查项：gs_check -i CheckCreateView
    3.在本地模式下运行openGauss的用户检查指定检查项：gs_check -i CheckCreateView -L
Expect      :
    1.执行成功
    2.合理报错
    3.本地模式下执行检查完成，结果不通过
History     : 
"""
import os
import unittest

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)
        self.pri_root = Node('PrimaryRoot')
        self.constant = Constant()
        self.script_path = os.path.join(
            os.path.dirname(macro.DB_INSTANCE_PATH), 'tool', 'script')

    def test_server_tools(self):
        text = '----step1: root用户进入安装目录下的script目录 expect: 成功----'
        self.log.info(text)
        self.log.info('在检查时指定目录')

        text = '----step2: 在非本地模式下运行openGauss的用户检查指定检查项 ' \
               'expect: 合理报错----'
        self.log.info(text)
        cmd = f'cd {self.script_path};' \
              f'./gs_check -i CheckCreateView'
        self.log.info(cmd)
        res = self.pri_root.sh(cmd).result()
        self.log.info(res)
        expect = '[GAUSS-53054]: ERROR: The command must be running ' \
                 'with cluster user'
        self.assertIn(expect, res, f'执行失败:{text}')

        text = '----step3: 在本地模式下运行openGauss的用户检查指定检查项 ' \
               'expect: 本地模式下执行检查完成，结果不通过----'
        self.log.info(text)
        cmd = f'cd {self.script_path};' \
              f'./gs_check -i CheckCreateView -L'
        self.log.info(cmd)
        res = self.pri_root.sh(cmd).result()
        self.log.info(res)
        self.assertIn('[RST] NG', res, f'执行失败:{text}')

    def tearDown(self):
        self.log.info('-----无需清理环境-----')
        text = f'-----{os.path.basename(__file__)} end-----'
        self.log.info(text)
